package com.ian.enumeration;

import com.ian.m3u8.exception.M3u8Exception;
import com.ian.util.StringUtils;
import lombok.AllArgsConstructor;
import lombok.Getter;

/**
 * @desc 媒体格式的枚举类
 * 
 * @author Witt
 * @version 1.0.0
 * @date 2022/11/10 23:30
 */
@Getter
@AllArgsConstructor
public enum MediaFormat {
    M3U8("m3u8"),
    MP4("mp4"),
    MKV("mkv"),
    WEBM("webm"),
    GIT("gif"),
    MOV("mov"),
    OGG("ogg"),
    FLV("flv"),
    AVI("avi"),
    TGP("3gp"),
    WMV("wmv"),
    MPG("mpg"),
    VOB("vob"),
    SWF("swf"),
    ;
    
    private String format;

    public static String getMediaFormat(String url) {
        if (!StringUtils.isUrl(url)) {
            throw new M3u8Exception(url + " 不是一个完整URL链接！");
        }
        url = url.substring(url.lastIndexOf("/") - 1);
        for (MediaFormat e : MediaFormat.values()) {
            if (url.contains(e.getFormat())) {
                return e.getFormat();
            }
        }
        throw new M3u8Exception("非视频链接！");
    }
}
